package com.component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;

/**
 * @author fanxiao  2022/3/29
 * @since 1.0.0
 */
public class CandidatesScannerConfigurer implements BeanDefinitionRegistryPostProcessor {

	private String basePackage;

	private Class<?> markerInterface;

	private Class<? extends CandidatesFactoryBean> factoryBeanClass;

	public void setFactoryBeanClass(Class<? extends CandidatesFactoryBean> factoryBeanClass) {
		this.factoryBeanClass = factoryBeanClass;
	}

	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}

	public void setMarkerInterface(Class<?> markerInterface) {
		this.markerInterface = markerInterface;
	}

	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		CandidatesClassPathScanner candidatesClassPathScanner = new CandidatesClassPathScanner(registry);
		candidatesClassPathScanner.setMarkerInterface(this.markerInterface);
		candidatesClassPathScanner.setFactoryBeanClass(this.factoryBeanClass);
		candidatesClassPathScanner.registerFilter();
		candidatesClassPathScanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

	}
}